<?php
class Controller_Admin_Manage extends FLEA_Controller_Action
{ 
	private $_ModelAdminProduct;
	private $_ModelSysUsers;
	private $user;
	private $siteInfo;

	function Controller_Admin_Manage (){
		$this -> _ModelAdminProduct = & FLEA::getSingleton('Model_AdminProduct');

		$rbac =& FLEA::getSingleton('FLEA_Rbac');	
		$this ->user = $rbac->getUser();//get login user's information

		$this->siteInfo = FLEA::getAppInf('siteInfo');//get site information

		$this->_ModelSysUsers = & FLEA::getSingleton('Model_SysUsers');
	}

	function actionIndex(){
		$page = isset ( $_GET ['page'] ) ? ( int ) $_GET ['page'] : 0;
		$pageSize = 20;
		$order = " id desc ";

		$this -> _ModelAdminProduct -> openDB('p_company');
		$this -> _ModelAdminProduct -> openDB('user');

		FLEA::loadHelper ( 'pager' );
		$pager =  new FLEA_Helper_Pager ( $this -> _ModelAdminProduct, $page, $pageSize, null, $order);
		$arranges = $pager->findAll ();
		//dump($arranges);exit;
		$viewData = array(
			'list' => $arranges,
			'Title' => '申报列表',
			'page' => $page, 
			'pagerData' =>$pager->getPagerData(),
		);
		//dump($viewData);exit;
		$this -> _executeView('Admin_Manage_List.html',$viewData);
	}

	function actionSearch(){
		$page = isset ( $_GET ['page'] ) ? ( int ) $_GET ['page'] : 0;
		$pageSize = 20;

		$username = $_POST['username']?$_POST['username']:$_GET['username'];
		$conditions = "username = '" . urldecode($username) . "'";

		$result = $this->_ModelSysUsers->find($conditions);
		$uid = $result['user_id'];

		if(!$uid){
			js_alert('用户不存在','',url('Admin_Manage'));
		}

		$this -> _ModelAdminProduct -> openDB('p_company');
		$this -> _ModelAdminProduct -> openDB('user');
		$cond = 'user_id = ' . $uid;

		FLEA::loadHelper ( 'pager' );
		$pager =  new FLEA_Helper_Pager ( $this -> _ModelAdminProduct, $page, $pageSize, $cond);
		$arranges = $pager->findAll ();
		//dump($arranges);exit;

		$viewData = array(
			'list' => $arranges,
			'Title' => '申报列表',
			'page' => $page, 
			'pagerData' =>$pager->getPagerData(),
			'keyword' => urlencode($username),
		);
		//dump($viewData);exit;
		$this -> _executeView('Admin_Manage_Search.html',$viewData);


	}

	function actionChooseUser(){
		$page = isset ( $_GET ['page'] ) ? ( int ) $_GET ['page'] : 0;
		$pageSize = 20;
		$order = " user_id ";

		FLEA::loadHelper ( 'pager' );
		$pager =  new FLEA_Helper_Pager ( $this->_ModelSysUsers, $page, $pageSize,null, $order);
		$arranges = $pager->findAll ();
		//		dump($arranges);exit;
		foreach ($arranges as $key=>$arrang) {
			$tmp=array();
			foreach ($arrang['roles'] as $roles) {
				//				dump($roles);exit;
				$tmp[]=$roles['rolename'];
				//dump();
			}
			$arranges[$key]['rolesname']=implode(',',$tmp);
		}

		//		dump($arranges);exit;
		$viewData = array (
		 'list' => $arranges,
		 'Title' => '选择用户', 
		 'page' => $page, 
		 'pagerData' =>$pager->getPagerData(),
		);
		//dump($viewdate);exit;
		$this->_executeView('Admin_User_Choose.html',$viewData);

	}

	function actionAdd(){
		$uid = $_GET['uid'];
		$class = FLEA::getAppInf('class');
		$viewData = array(
			'class' => $class,
			'Title' => '选择大类',
			'uid' => $uid,
		);
		$this -> _executeView('Admin_Nav.html',$viewData);
	}

	function actionAddNext(){
		$uid = $_GET['uid'];
		$material = FLEA::getAppInf('material');
		$isForeign = FLEA::getAppInf('isForeign');
		$class = FLEA::getAppInf('class');
		$pack = FLEA::getAppInf('pack');
		$companyNature = FLEA::getAppInf('companyNature');
		
		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}

		$num = count($classArray['directoryName']);

		$viewData = array(
			'siteInfo'=>$this->siteInfo,
			'material'=>$material,
			'isForeign'=>$isForeign,
			'classArray'=>$classArray,
			'class'=>$class,
			'pack'=>$pack,
			'companyNature'=>$companyNature,
			'findId'=>$kind,
			'num'=>$num,
			'Title'=>'添加申报',
			'uid' => $uid,
			'do' => 'add',
		);
		//dump($viewData);exit;
		$this -> _executeView('Admin_Product_Add.html',$viewData);
		
	}

	function actionAddMore(){
		$uid = $_GET['uid'];
		foreach($_GET as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_GET[$_key];
		}
		$class = FLEA::getAppInf('class');
		$_ModelTemporary = FLEA::getSingleton('Model_Temporary');
		
		$Modelrand = & FLEA::getSingleton('Model_Code');
		$randnew = $Modelrand ->getCode(5);
		$timenew = time();
		
		if($rand && $time){
			$conditions = 'mark = "' . $uid . '+' . $time . '+' . $rand . '"';
			$field = 'mark';
			$value = $uid . '+' . $timenew . '+' . $randnew;
			$_ModelTemporary->updateField($conditions, $field, $value);
		}
		
		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}
		$num = count($classArray['directoryName']);
		$viewData = array(
			'siteInfo' => $this->siteInfo, 
			'user_id' => $uid,
			'time' => $timenew,
			'rand' => $randnew,
			'class'=> $class,
			'classArray'=>$classArray,
			'num'=>$num,
		);
//		dump($viewData);exit;
		$this->_executeView('AddNumber.html', $viewData);
	}

	function actionModify(){
		$user_id = $_GET['uid'];
		$material = FLEA::getAppInf('material');
		$isForeign = FLEA::getAppInf('isForeign');
		$class = FLEA::getAppInf('class');
		$pack = FLEA::getAppInf('pack');
		$companyNature = FLEA::getAppInf('companyNature');

		$conditions = 'id = ' . $_GET['id'];

		$this->_ModelAdminProduct -> openDB('cert');
		$this->_ModelAdminProduct -> openDB('p_company');
		$this->_ModelAdminProduct -> openDB('subitem');

		$list = $this->_ModelAdminProduct -> find($conditions);

		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}

		$viewData = array(
			'id' => $_GET['id'],
			'list' => $list,
			'do' => 'edit',
			'material'=>$material,
			'isForeign'=>$isForeign,
			'classArray'=>$classArray,
			'class'=>$class,
			'pack'=>$pack,
			'companyNature'=>$companyNature,
			'Title' => '编辑用户申报',
			'user_id' => $user_id,
			'siteInfo' => $this->siteInfo,
		);
//		dump($viewData);exit;
		$this -> _executeView('Admin_Product_Edit.html',$viewData);
	}

	function actionShow(){
		is_numeric($_GET['id'])?$id = $_GET['id']:redirect(url('Admin_Manage'));
		$this ->_ModelProduct = & FLEA::getSingleton('Model_Product');
		$conditions = "id = " . $id;

		$_ModelProductSubitem = FLEA::getSingleton('Model_ProductSubitem');
		$conditions = 'product_id = ' . $id;
		$list = $_ModelProductSubitem -> findAll($conditions);

		$class = FLEA::getAppInf('class');
		$_GET['kind']?$kind = $_GET['kind']:$kind = 1;
		
		switch($kind){
			case 1:$classArray = $class[0];
			break;
			case 2:$classArray = $class[1];
			break;
			case 3:$classArray = $class[2];
			break;
		}
//		dump($list);exit;
		foreach ($list as $key => $value){
			$tmp = explode('|',$value['package']);
			
			$classTmp = explode(':',$tmp[0]);
			$moreTmp = explode(',',$classTmp[1]);
			$list[$key]['package'] = array('class'=>$classTmp[0],'more'=>$moreTmp);	
			
			$selfTmp = explode(',',$tmp[1]);
			$list[$key]['package']['self'] = $selfTmp[0];
			$list[$key]['package']['other'] = $selfTmp[1];
			
			$idTmp = explode('_',$selfTmp[0]);
			$list[$key]['package']['id'] = $idTmp[0];
			$list[$key]['package']['rank'] = $idTmp[1];
		}
		$num = count($classArray['directoryName']);
		$count = count($list);
//		dump($list);exit;
		$viewData = array(
			'list' => $list,
			'class'=> $class,
			'classArray'=>$classArray,
			'do'=>'add',
			'siteInfo' => $this->siteInfo,
			'num'=>$num,
			'product_id'=>$id,
			'kind'=>$kind,
			'start_id'=>$count + 1,
		);
//		dump($viewData);exit;
		$this->_executeView('ShowNumber.html', $viewData);
		
	}

	function actionEdit(){
		foreach($_POST as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_POST[$_key];
		}
		//dump($_POST);exit;
		$_ModelProductSubitem = FLEA::getSingleton('Model_ProductSubitem');
		$conditions = 'product_id = ' . $product_id;

		$_ModelProductSubitem -> removeByConditions($conditions);

		foreach($productNumber as $keys => $loop){
			$packSelect = 'packages'.$keys;
//			dump($packSelect);exit;
			$package = $classArray[$keys] . ":" . implode(",",$$packSelect);
			if(empty($package)){
				js_alert('组套为必填','',url('Admin_Manage','Show&id=$product&kind='));
			}
			if($self[$keys] && $other[$keys]){
				$package .= '|'. $self[$keys] . ',' . $other[$keys];
			}
			$list[]=array(
				'productNumber' => $productNumber[$keys],
				'format' => $format[$keys],
				'model' => $model[$keys],
				'outland1' => $outland1[$keys],
				'outland2' => $outland2[$keys],
				'outland3' => $outland3[$keys],
				'outland4' => $outland4[$keys],
				'hospital1_name' => $hospital1_name[$keys],
				'hospital1_price' => $hospital1_price[$keys],
				'hospital1_person' => $hospital1_person[$keys],
				'hospital1_telephone' => $hospital1_telephone[$keys],
				'hospital2_name' => $hospital1_name[$keys],
				'hospital2_price' => $hospital1_price[$keys],
				'hospital2_person' => $hospital1_person[$keys],
				'hospital2_telephone' => $hospital1_telephone[$keys],
				'hospital3_name' => $hospital1_name[$keys],
				'hospital3_price' => $hospital1_price[$keys],
				'hospital3_person' => $hospital1_person[$keys],
				'hospital3_telephone' => $hospital1_telephone[$keys],
				'package'=>$package,
				'product_id' => $product_id,
			);
		}
//		dump($list);exit;
		$_ModelProductSubitem -> saveRowset($list);
		js_alert('修改成功','window.close()','');
	}

	function actionDelete(){
		$page = isset ( $_POST ['page'] ) ? ( int ) $_POST ['page'] : 0;
		$ids = $_POST ['ids'];
		
		$this->_ModelAdminProduct -> openDB('cert');
		$this->_ModelAdminProduct -> openDB('p_company');
		if(!empty($ids)){
			foreach ($ids as $id){
				$this->_ModelAdminProduct -> removeByPkv($id);
			}
		}
		js_alert("删除成功",'',$this->_url("Index",array('page'=>$page)));

	}

	function actionChange(){
//		dump($_POST);
		$user_id = $_POST['user_id'];

		foreach($_POST as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_POST[$_key];
		}

		$p_company_column = array('nature','name','permitNumber','permitAddress','permitFunds','permitRealfunds','permitValidDate','licenceNumber','licenceAddress','licenceArea','licenceValidDate');
		$cert_column = array('oldNumber','oldValidDate','noteNumber','noteValidDate','newNumber','newValidDate','FDAnumber','FDAvalidDate','ECnumber','ECvalidDate','CMDproductNumber','CMDprodactDate','CMDqualityNumber','CMDqualityDate','CMDproductISOnumber','CMDproductISOdate',); 
		
		$list = array(
			'name' => $name,
			'range' => $range,
			'part' => $part,
			'norm' => $norm,
			'pack'=> $pack,
			'sterilizing'=> $sterilizing,
			'position'=>$position,
			'positionMore'=>$positionMore,
			'directoryName'=>$directoryName,
			'clinicalName'=>$clinicalName,
			'material'=>$material,
			'materialMore'=>$materialMore,
			'brand'=>$brand,
			'location'=>$location,
			'isForeign'=>$origin,
			'findId'=>$findId,
			'product_c'=>$product_c,
			'user_id'=>$user_id,
			//'package'=>$package,			
		);
		
		if(isset($cert) || isset($cert_for)){
//			dump($_POST['oldNumber']);
			foreach ($cert as $c_key => $c_value){
				if(empty($c_value)&&empty($cert_for[$c_key]))continue;
				$name = $cert_column[$c_key];
				$list['cert'][$name] = $cert_for[$c_key]?addslashes($cert_for[$c_key]):$c_value; 
			}
		}
		
		if(isset($p_company)){
			foreach ($p_company as $p_key => $p_value){
				if(empty($p_value))continue;
				$name = $p_company_column[$p_key];
				$list['p_company'][$name] = $p_value; 
			}
		}
//		dump($list);exit;	
		if($do == 'add'){
			$list['createtime'] = date('Y-m-d H:i:s');
			$msg = '添加成功';
		}elseif($do == 'edit'){
			$list['id'] = $_POST['id'];
			$msg = '修改成功';
		}
		
//		dump($list);exit;
		$this->_ModelAdminProduct -> openDB('cert');
		$this->_ModelAdminProduct -> openDB('p_company');
		$product_id = $this ->_ModelAdminProduct->save($list);
		$_ModelProductSubitem = FLEA::getSingleton('Model_ProductSubitem');

		if($do == 'edit'){
			js_alert($msg,'',url('Admin_Manage','Index'));
			die;
		}

		$conditions = 'mark = "' . $user_id . '+' . $time . '+' . $rand . '"';
		$_ModelTemporary = FLEA::getSingleton('Model_Temporary');
		$arrangs = $_ModelTemporary -> findAll($conditions);
		
//		dump($arrangs);exit;
		
		foreach($arrangs as $key => $arrang){
			unset($arrangs[$key]['mark']);
			$arrangs[$key]['product_id'] = $product_id;
		}
//		dump($arrangs);exit;
		$_ModelProductSubitem -> createRowset($arrangs);	
		js_alert($msg,'',url('Admin_Manage','Index'));	
	}

	function actionTemporary(){
//		dump($_POST);exit;
		foreach($_POST as $_key=>$_value){
			!ereg("^\_[A-Z]+",$_key) && $$_key=$_POST[$_key];
		}

//		dump($hospital3_name);exit;
		
		foreach($productNumber as $keys => $loop){
			if(empty($model[$keys])&&empty($format[$keys])){
				js_alert('请填写内容后提交','',url('Admin_Manage','AddMore'));
			}
			$packSelect = 'packages'.($keys + 1);
//			dump($packSelect);exit;
			$package = $classArray[$keys] . ":" . implode(",",$$packSelect);
			if(empty($package)){
				js_alert('组套为必填','',url('Admin_Manage','AddMore'));
			}
			if($self[$keys] && $other[$keys]){
				$package .= '|'. $self[$keys] . ',' . $other[$keys];
			}
			$list[]=array(
				'productNumber' => $productNumber[$keys],
				'format' => $format[$keys],
				'model' => $model[$keys],
				'outland1' => $outland1[$keys],
				'outland2' => $outland2[$keys],
				'outland3' => $outland3[$keys],
				'outland4' => $outland4[$keys],
				'hospital1_name' => $hospital1_name[$keys],
				'hospital1_price' => $hospital1_price[$keys],
				'hospital1_person' => $hospital1_person[$keys],
				'hospital1_telephone' => $hospital1_telephone[$keys],
				'hospital2_name' => $hospital1_name[$keys],
				'hospital2_price' => $hospital1_price[$keys],
				'hospital2_person' => $hospital1_person[$keys],
				'hospital2_telephone' => $hospital1_telephone[$keys],
				'hospital3_name' => $hospital1_name[$keys],
				'hospital3_price' => $hospital1_price[$keys],
				'hospital3_person' => $hospital1_person[$keys],
				'hospital3_telephone' => $hospital1_telephone[$keys],
				'mark' => $_POST['user_id'] . '+' .  $_POST['time'] . '+' . $_POST['rand'],
				'package'=>$package,
			);
		}
//		dump($list);exit;
		$_ModelTemporary = FLEA::getSingleton('Model_Temporary');
		$_ModelTemporary-> saveRowset($list);
		
		js_alert('添加成功','window.close()','');
	}
}
?>